{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/moemen/Projects/TFSegmentation\nTESTING THE RESNET18 Model\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pretrained weights loaded\nBuilding the RESNET..\nBuilding unit: conv1\nconv1-shape: [1, 56, 56, 64]\nBuilding residual unit: conv2_1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "residual-unit-conv2_1-shape: [1, 56, 56, 64]\nBuilding residual unit: conv2_2\nresidual-unit-conv2_2-shape: [1, 56, 56, 64]\nBuilding residual unit: conv3_1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "residual-unit-conv3_1-shape: [1, 28, 28, 128]\nBuilding residual unit: conv3_2\nresidual-unit-conv3_2-shape: [1, 28, 28, 128]\nBuilding residual unit: conv4_1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "residual-unit-conv4_1-shape: [1, 14, 14, 256]\nBuilding residual unit: conv4_2\nresidual-unit-conv4_2-shape: [1, 14, 14, 256]\nBuilding residual unit: conv5_1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "residual-unit-conv5_1-shape: [1, 7, 7, 512]\nBuilding residual unit: conv5_2\nresidual-unit-conv5_2-shape: [1, 7, 7, 512]\nBuilding unit: logits\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logits-shape: [1, 1000]\n\nEncoder RESNET is built successfully\n\n\nLoading pretrained weights of resnet18\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All pretrained weights of resnet18 is loaded\n[]\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "print(os.getcwd())\n",
    "\n",
    "print(\"TESTING THE RESNET18 Model\")\n",
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "\n",
    "# init the graph\n",
    "tf.reset_default_graph()\n",
    "sess = tf.Session()\n",
    "init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())\n",
    "\n",
    "# Set some parameters\n",
    "x_input = tf.placeholder(tf.float32, [1, 224, 224, 3])\n",
    "num_classes = 1000\n",
    "pretrained_path = 'pretrained_weights/resnet18.npy'\n",
    "train_flag = tf.placeholder(tf.bool)\n",
    "weight_decay = 5e-4\n",
    "\n",
    "from models.encoders.resnet_18 import RESNET18\n",
    "\n",
    "resnet = RESNET18(x_input, num_classes, pretrained_path, train_flag, weight_decay)\n",
    "resnet.build()\n",
    "\n",
    "sess.run(init)\n",
    "\n",
    "resnet.load_pretrained_weights(sess)\n",
    "\n",
    "# check if any variables is not initialized\n",
    "uninit = sess.run(tf.report_uninitialized_variables())\n",
    "print(uninit)\n",
    "assert not uninit, ':(, :('\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 224, 224, 3)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[283]\nHurray RESNET 18 is WORKING!!!!\n"
     ]
    }
   ],
   "source": [
    "import scipy as sc\n",
    "import numpy as np\n",
    "\n",
    "img = sc.misc.imread('models/encoders/tests/cat.jpg')\n",
    "img = np.expand_dims(img, axis=0)\n",
    "print(img.shape)\n",
    "\n",
    "x = sess.run(resnet.score, {x_input: img, train_flag: False})\n",
    "ans = np.argmax(x, axis=1)\n",
    "print(ans)\n",
    "\n",
    "assert ans == 281 or 282 or 283 or 284 or 285, 'it is not a cat'\n",
    "\n",
    "print(\"Hurray RESNET 18 is WORKING!!!!\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import pickle\n",
    "import os\n",
    "import torchfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# init the graph\n",
    "tf.reset_default_graph()\n",
    "sess = tf.Session()\n",
    "init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())\n",
    "\n",
    "# Set some parameters\n",
    "x_input = tf.placeholder(tf.float32, [1, 224, 224, 3])\n",
    "num_classes = 1000\n",
    "pretrained_path = 'pretrained_weights/resnet18.npy'\n",
    "train_flag = tf.placeholder(tf.bool)\n",
    "weight_decay = 5e-4\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pretrained weights loaded\nBuilding the RESNET..\nBuilding unit: conv1\nconv1-shape: [1, 56, 56, 64]\nBuilding residual unit: conv2_1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "residual-unit-conv2_1-shape: [1, 56, 56, 64]\nBuilding residual unit: conv2_2\nresidual-unit-conv2_2-shape: [1, 56, 56, 64]\nBuilding residual unit: conv3_1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "residual-unit-conv3_1-shape: [1, 28, 28, 128]\nBuilding residual unit: conv3_2\nresidual-unit-conv3_2-shape: [1, 28, 28, 128]\nBuilding residual unit: conv4_1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "residual-unit-conv4_1-shape: [1, 14, 14, 256]\nBuilding residual unit: conv4_2\nresidual-unit-conv4_2-shape: [1, 14, 14, 256]\nBuilding residual unit: conv5_1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "residual-unit-conv5_1-shape: [1, 7, 7, 512]\nBuilding residual unit: conv5_2\nresidual-unit-conv5_2-shape: [1, 7, 7, 512]\nBuilding unit: logits\nlogits-shape: [1, 1000]\n\nEncoder RESNET is built successfully\n\n\n"
     ]
    }
   ],
   "source": [
    "from models.encoders.resnet_18 import RESNET18\n",
    "\n",
    "\n",
    "resnet = RESNET18(x_input, num_classes, pretrained_path, train_flag, weight_decay)\n",
    "resnet.build()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "printing the names of all variables and their shapes\nconv1_x/conv1/weights:(7, 7, 3, 64)\nconv1_x/bn1/beta:(64,)\nconv1_x/bn1/gamma:(64,)\nconv2_x/conv2_1/conv_1/weights:(3, 3, 64, 64)\nconv2_x/conv2_1/bn_1/beta:(64,)\nconv2_x/conv2_1/bn_1/gamma:(64,)\nconv2_x/conv2_1/conv_2/weights:(3, 3, 64, 64)\nconv2_x/conv2_1/bn_2/beta:(64,)\nconv2_x/conv2_1/bn_2/gamma:(64,)\nconv2_x/conv2_2/conv_1/weights:(3, 3, 64, 64)\nconv2_x/conv2_2/bn_1/beta:(64,)\nconv2_x/conv2_2/bn_1/gamma:(64,)\nconv2_x/conv2_2/conv_2/weights:(3, 3, 64, 64)\nconv2_x/conv2_2/bn_2/beta:(64,)\nconv2_x/conv2_2/bn_2/gamma:(64,)\nconv3_x/conv3_1/shortcut_conv/weights:(1, 1, 64, 128)\nconv3_x/conv3_1/conv_1/weights:(3, 3, 64, 128)\nconv3_x/conv3_1/bn_1/beta:(128,)\nconv3_x/conv3_1/bn_1/gamma:(128,)\nconv3_x/conv3_1/conv_2/weights:(3, 3, 128, 128)\nconv3_x/conv3_1/bn_2/beta:(128,)\nconv3_x/conv3_1/bn_2/gamma:(128,)\nconv3_x/conv3_2/conv_1/weights:(3, 3, 128, 128)\nconv3_x/conv3_2/bn_1/beta:(128,)\nconv3_x/conv3_2/bn_1/gamma:(128,)\nconv3_x/conv3_2/conv_2/weights:(3, 3, 128, 128)\nconv3_x/conv3_2/bn_2/beta:(128,)\nconv3_x/conv3_2/bn_2/gamma:(128,)\nconv4_x/conv4_1/shortcut_conv/weights:(1, 1, 128, 256)\nconv4_x/conv4_1/conv_1/weights:(3, 3, 128, 256)\nconv4_x/conv4_1/bn_1/beta:(256,)\nconv4_x/conv4_1/bn_1/gamma:(256,)\nconv4_x/conv4_1/conv_2/weights:(3, 3, 256, 256)\nconv4_x/conv4_1/bn_2/beta:(256,)\nconv4_x/conv4_1/bn_2/gamma:(256,)\nconv4_x/conv4_2/conv_1/weights:(3, 3, 256, 256)\nconv4_x/conv4_2/bn_1/beta:(256,)\nconv4_x/conv4_2/bn_1/gamma:(256,)\nconv4_x/conv4_2/conv_2/weights:(3, 3, 256, 256)\nconv4_x/conv4_2/bn_2/beta:(256,)\nconv4_x/conv4_2/bn_2/gamma:(256,)\nconv5_x/conv5_1/shortcut_conv/weights:(1, 1, 256, 512)\nconv5_x/conv5_1/conv_1/weights:(3, 3, 256, 512)\nconv5_x/conv5_1/bn_1/beta:(512,)\nconv5_x/conv5_1/bn_1/gamma:(512,)\nconv5_x/conv5_1/conv_2/weights:(3, 3, 512, 512)\nconv5_x/conv5_1/bn_2/beta:(512,)\nconv5_x/conv5_1/bn_2/gamma:(512,)\nconv5_x/conv5_2/conv_1/weights:(3, 3, 512, 512)\nconv5_x/conv5_2/bn_1/beta:(512,)\nconv5_x/conv5_2/bn_1/gamma:(512,)\nconv5_x/conv5_2/conv_2/weights:(3, 3, 512, 512)\nconv5_x/conv5_2/bn_2/beta:(512,)\nconv5_x/conv5_2/bn_2/gamma:(512,)\nlogits/logits_dense/weights:(512, 1000)\nlogits/logits_dense/biases:(1000,)\nconv1_x/bn1/mu:(64,)\nconv1_x/bn1/sigma:(64,)\nconv2_x/conv2_1/bn_1/mu:(64,)\nconv2_x/conv2_1/bn_1/sigma:(64,)\nconv2_x/conv2_1/bn_2/mu:(64,)\nconv2_x/conv2_1/bn_2/sigma:(64,)\nconv2_x/conv2_2/bn_1/mu:(64,)\nconv2_x/conv2_2/bn_1/sigma:(64,)\nconv2_x/conv2_2/bn_2/mu:(64,)\nconv2_x/conv2_2/bn_2/sigma:(64,)\nconv3_x/conv3_1/bn_1/mu:(128,)\nconv3_x/conv3_1/bn_1/sigma:(128,)\nconv3_x/conv3_1/bn_2/mu:(128,)\nconv3_x/conv3_1/bn_2/sigma:(128,)\nconv3_x/conv3_2/bn_1/mu:(128,)\nconv3_x/conv3_2/bn_1/sigma:(128,)\nconv3_x/conv3_2/bn_2/mu:(128,)\nconv3_x/conv3_2/bn_2/sigma:(128,)\nconv4_x/conv4_1/bn_1/mu:(256,)\nconv4_x/conv4_1/bn_1/sigma:(256,)\nconv4_x/conv4_1/bn_2/mu:(256,)\nconv4_x/conv4_1/bn_2/sigma:(256,)\nconv4_x/conv4_2/bn_1/mu:(256,)\nconv4_x/conv4_2/bn_1/sigma:(256,)\nconv4_x/conv4_2/bn_2/mu:(256,)\nconv4_x/conv4_2/bn_2/sigma:(256,)\nconv5_x/conv5_1/bn_1/mu:(512,)\nconv5_x/conv5_1/bn_1/sigma:(512,)\nconv5_x/conv5_1/bn_2/mu:(512,)\nconv5_x/conv5_1/bn_2/sigma:(512,)\nconv5_x/conv5_2/bn_1/mu:(512,)\nconv5_x/conv5_2/bn_1/sigma:(512,)\nconv5_x/conv5_2/bn_2/mu:(512,)\nconv5_x/conv5_2/bn_2/sigma:(512,)\n"
     ]
    }
   ],
   "source": [
    "sess.run(init)\n",
    "\n",
    "print('printing the names of all variables and their shapes')\n",
    "all_vars = tf.trainable_variables()\n",
    "all_vars += tf.get_collection('mu_sigma_bn')\n",
    "for v in all_vars:\n",
    "    print(v.op.name + \":\" + str(v.shape))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/moemen/Projects/TFSegmentation\n",
      "assigning pretrained weights\n",
      "conv1_x/conv1/weights:(7, 7, 3, 64)\n",
      "conv1_x/bn1/beta:(64,)\n",
      "conv1_x/bn1/gamma:(64,)\n",
      "conv2_x/conv2_1/conv_1/weights:(3, 3, 64, 64)\n",
      "conv2_x/conv2_1/bn_1/beta:(64,)\n",
      "conv2_x/conv2_1/bn_1/gamma:(64,)\n",
      "conv2_x/conv2_1/conv_2/weights:(3, 3, 64, 64)\n",
      "conv2_x/conv2_1/bn_2/beta:(64,)\n",
      "conv2_x/conv2_1/bn_2/gamma:(64,)\n",
      "conv2_x/conv2_2/conv_1/weights:(3, 3, 64, 64)\n",
      "conv2_x/conv2_2/bn_1/beta:(64,)\n",
      "conv2_x/conv2_2/bn_1/gamma:(64,)\n",
      "conv2_x/conv2_2/conv_2/weights:(3, 3, 64, 64)\n",
      "conv2_x/conv2_2/bn_2/beta:(64,)\n",
      "conv2_x/conv2_2/bn_2/gamma:(64,)\n",
      "conv3_x/conv3_1/shortcut_conv/weights:(1, 1, 64, 128)\n",
      "conv3_x/conv3_1/conv_1/weights:(3, 3, 64, 128)\n",
      "conv3_x/conv3_1/bn_1/beta:(128,)\n",
      "conv3_x/conv3_1/bn_1/gamma:(128,)\n",
      "conv3_x/conv3_1/conv_2/weights:(3, 3, 128, 128)\n",
      "conv3_x/conv3_1/bn_2/beta:(128,)\n",
      "conv3_x/conv3_1/bn_2/gamma:(128,)\n",
      "conv3_x/conv3_2/conv_1/weights:(3, 3, 128, 128)\n",
      "conv3_x/conv3_2/bn_1/beta:(128,)\n",
      "conv3_x/conv3_2/bn_1/gamma:(128,)\n",
      "conv3_x/conv3_2/conv_2/weights:(3, 3, 128, 128)\n",
      "conv3_x/conv3_2/bn_2/beta:(128,)\n",
      "conv3_x/conv3_2/bn_2/gamma:(128,)\n",
      "conv4_x/conv4_1/shortcut_conv/weights:(1, 1, 128, 256)\n",
      "conv4_x/conv4_1/conv_1/weights:(3, 3, 128, 256)\n",
      "conv4_x/conv4_1/bn_1/beta:(256,)\n",
      "conv4_x/conv4_1/bn_1/gamma:(256,)\n",
      "conv4_x/conv4_1/conv_2/weights:(3, 3, 256, 256)\n",
      "conv4_x/conv4_1/bn_2/beta:(256,)\n",
      "conv4_x/conv4_1/bn_2/gamma:(256,)\n",
      "conv4_x/conv4_2/conv_1/weights:(3, 3, 256, 256)\n",
      "conv4_x/conv4_2/bn_1/beta:(256,)\n",
      "conv4_x/conv4_2/bn_1/gamma:(256,)\n",
      "conv4_x/conv4_2/conv_2/weights:(3, 3, 256, 256)\n",
      "conv4_x/conv4_2/bn_2/beta:(256,)\n",
      "conv4_x/conv4_2/bn_2/gamma:(256,)\n",
      "conv5_x/conv5_1/shortcut_conv/weights:(1, 1, 256, 512)\n",
      "conv5_x/conv5_1/conv_1/weights:(3, 3, 256, 512)\n",
      "conv5_x/conv5_1/bn_1/beta:(512,)\n",
      "conv5_x/conv5_1/bn_1/gamma:(512,)\n",
      "conv5_x/conv5_1/conv_2/weights:(3, 3, 512, 512)\n",
      "conv5_x/conv5_1/bn_2/beta:(512,)\n",
      "conv5_x/conv5_1/bn_2/gamma:(512,)\n",
      "conv5_x/conv5_2/conv_1/weights:(3, 3, 512, 512)\n",
      "conv5_x/conv5_2/bn_1/beta:(512,)\n",
      "conv5_x/conv5_2/bn_1/gamma:(512,)\n",
      "conv5_x/conv5_2/conv_2/weights:(3, 3, 512, 512)\n",
      "conv5_x/conv5_2/bn_2/beta:(512,)\n",
      "conv5_x/conv5_2/bn_2/gamma:(512,)\n",
      "logits/logits_dense/weights:(512, 1000)\n",
      "logits/logits_dense/biases:(1000,)\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "print(os.getcwd())\n",
    "model_weights = np.load('utils/resnet18.npy').item()\n",
    "print('assigning pretrained weights')\n",
    "all_vars = tf.trainable_variables()\n",
    "all_vars += tf.get_collection('mu_sigma_bn')\n",
    "for v in all_vars:\n",
    "    assign_op = v.assign(model_weights[v.op.name])\n",
    "    sess.run(assign_op)\n",
    "    print(v.op.name + \":\" + str(v.shape))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
